<?php

/**
 * 特批发票控制器
 * @author yuzy 2015-5-13
 */

namespace mybackend\controllers;

use Yii;
use common\models\myhtsq\SpecialApprovalAuthorizeModel;
use common\services\myhtsq\SpecialApprovalAuthorizeService;
use common\entities\myhtsq\ActionCode;

class SpecialApprovalAuthorizeController extends BaseController {

    protected $_authorizeService;

    public function __construct($id, $module, SpecialApprovalAuthorizeService $authorizeService, $config = []) {
        $this->_authorizeService = $authorizeService;
        parent::__construct($id, $module, $config);
    }
    
    protected function roleAuthRules(){
        return [
            'actions'=>['GetSpecialApprovalAuthorizeList','actionGetSpecialApprovalAuthorizeInfo','SaveSpecialApprovalAuthorize'],
            'rules'=>[
                        [
                        'allow'=>true,
                        'actionCodeMaps'=>[
                            'GetSpecialApprovalAuthorizeList'=>ActionCode::SPECIAL_APPROVE_AUTHORIZE_VIEW,
                            'actionGetSpecialApprovalAuthorizeInfo'=>ActionCode::SPECIAL_APPROVE_AUTHORIZE_VIEW,
                            'SaveSpecialApprovalAuthorize'=>ActionCode::SPECIAL_APPROVE_AUTHORIZE_MNG,
                            ]
                        ]
                    ]
                ];
    }

    /**
     * 特批发票列表
     * @param integer $page 页码
     * @param integer $pageSize 页条数
     * @param string $sortBy 排序字段
     * @param boolean $sortAsc 是否升序
     * @param integer $areaId 区域id
     * @param string $keyword 关键字
     * @return array
     */
    public function actionGetSpecialApprovalAuthorizeList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId,$fulfil) {
        $result = $this->_authorizeService->getSpecialApprovalAuthorizeList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId,$fulfil);
        $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
    }

    /**
     * 获取特批发票信息
     * @param string $authorizeId
     * @return \common\models\myhtsq\SpecialApprovalAuthorizeModel
     */
    public function actionGetSpecialApprovalAuthorizeInfo($authorizeId){
        $result = $this->_authorizeService->getSpecialApprovalAuthorizeInfo($authorizeId);
        $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg()); 
    }    
    
    /**
     * 保存特批发票
     */
    public function actionSaveSpecialApprovalAuthorize() {
        $this->exitIfNotPost();
        $model = new SpecialApprovalAuthorizeModel();
        if ($model->load($_POST['item'],'')) {
            $attachmentList = [];
            if(isset($_POST['attachmentList'])){
                $attachmentList = $_POST['attachmentList'];
            }
            if ($model->special_approval_authorize_id == '') {
                $result = $this->_authorizeService->addSpecialApprovalAuthorize($model,$attachmentList);
            } else {
                $result = $this->_authorizeService->updateSpecialApprovalAuthorize($model,$attachmentList);
            }
            
            $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
        } else {
            $this->errorParam();
        }
    }

}
